/**
 * Created by zhoushg on 2017/8/23.
 */
import fetch from '../../utils/request'
import Vue from 'vue'
import Vuex from 'vuex'
import store from '../../store/index'
import {toast} from '../../utils/gui_main'

let getData = (params, callBack, loading) => {
  if (loading) {
    store.state.app.ajaxLoading = true
  }
  fetch(params).then(function (data) {
    if (loading) {
      store.state.app.ajaxLoading = false
    }
    data = data.data
	
    let status = data.returnFlg
    let result = data.data
	 if (callBack) {
        callBack(result)
      }
	  return // 后续不走了 该有的弹窗都交个了request.js 来处理
    if (status) {
      if (!result) {
        result = true
      }
      if (callBack) {
        callBack(result)
      }
    } else {
      var errMsg = data.errMsg
      if (typeof errMsg === 'object') {
        if (errMsg.errId === 1) {
          toast.error(errMsg.msgStr)
          store.dispatch('LogOut', '').then(data => {
          })
        }
      } else {
        toast.error(data.errMsg)
      }
      if (callBack) callBack(false)
    }
  }).catch(function (error) {
    if (loading) {
      store.state.app.ajaxLoading = false
    }
    // toast.error('请求错误 ，请稍后再试~~~')
    if (callBack) {
      callBack(false)
    }
  })
}

export function getUserId() {
  return store.state.user.userid
}

export default getData
